$(function() {
	$('tbody')
			.on(
					'click','.btn-primary-save',

					function() {
						var formData = {};
						var $tr = $(this).parents('tr');
						noticeId = $tr.find("input[name='id']").val();
						$cnName = $tr.find("input[name='cn_name']");
						
						if($cnName.val().trim() ==""){
							$cnName.tooltip({
								trigger: 'nanual',
								title: '不能为空'
							}).tooltip('show').one('focus',function(){
								$(this).tooltip('hide');
							});
							return false;
						}
						var url = "/web/category/editCategory";
						formData['id'] = noticeId;
						if (noticeId == undefined) {
							formData['id'] = noticeId;
							url = "/web/category/addCategory";
						}
						formData['enName'] = $("#enName").val();
						formData['cnName'] = $cnName.val();
						cAjax({
							url : url,
							type : 'post',
							data : formData,
							success : function(obj) {
								var strId = "<input type='hidden' name ='id' value = '" +obj.data+ "'>";
								var str = "<td><input type='text' class='form-control' value='"+ formData['cnName']+ "' name='cn_name'/></td><td><a href='javascript:;' class='btn btn-xs btn-primary btn-primary-save'>保存</a> <a href='javascript:;' class='btn btn-xs btn-danger notice-del-btn'>删除</a></td>";
								if(noticeId != undefined){
									$tr.empty();
									$tr.append(strId);
									$tr.append(str);
								}else{
									$tr.before("<tr>"+strId+str+"</tr>");
									$("#cnName").val("");
								}
							},
							error : function(obj) {
								console.log(obj);
							}
						});
					});

	$('tbody')
			.on(
					'click','.notice-del-btn',
					function() {
						var $this = $(this);
						actionConfirm({
							msg : "确定要删除吗？",
							confirm : function() {
								cAjax({
									url : '/web/category/deleteCategory',
									type : 'post',
									data : {
										categoryId : $this.parents('tr').find("input[name='id']").val()
									},
									success : function(obj) {
										$this.parents('tr').addClass('deleted');
										$this
												.parent()
												.html(
														'<span class="label label-warning">已删除</span>');
									},
									error : function(obj) {
									}
								});
							}
						});
					});
});
